package cn.qyd.kaoshi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author 邱运铎
 * @date 2024-03-22 10:17
 */
public class DepthSearchDemo01 {
    private static StringBuilder builder = new StringBuilder();
    private static int index ;
    private static HashSet<Integer> hset = new HashSet<>();
    public static void main(String[] args) {
        DepthSearchDemo01 demo = new DepthSearchDemo01();
//        String s = "barfoofoobarthefoobarman";
        String s = "barfoofoobarthefoobarman";
        String[] words = new String[]{"bar","foo","the"};
        List<Integer> substring = demo.findSubstring(s, words);
        System.out.println("----");
        System.out.println(substring);
        System.out.println("++++");
        System.out.println(hset);

    }
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> result = new ArrayList<>();
        dfs(hset,s,words,0);
        hset.forEach(item -> result.add(item));
        return result;
    }
    public void dfs(HashSet<Integer> hset , String s, String[] words, int begin) {
        if (begin == words.length) {
            Stream.of(words).forEach(item -> builder.append(item));
            int start = 0;
            while((index = s.indexOf(builder.toString(),start)) >= 0) {
                System.out.println(index);
                hset.add(index);
                start = ++index;
                System.out.println("start:= "+ start);
            }
            builder.setLength(0);
            return;
        }

        for (int i = begin; i < words.length; i++) {
            swap(words,begin,i);
            dfs(hset,s,words,begin+1);
            swap(words,begin,i);
        }
    }

    public void swap(String[] temps, int left, int right) {
        String temp = temps[left];
        temps[left] = temps[right];
        temps[right] = temp;
    }
}